package com.bart.hsp.scala.ch10.sec04

trait Logger {
  def log(msg: String);
}
// 继承Logger
trait ConsoleLogger extends Logger {
  def log(msg: String) { println(f"from ConsoleLogger $msg")}
}

class Account {
  protected var balance = 0.0
}

abstract class SavingsAccount extends Account with Logger {
  def withdraw(amount: Double) {
    if (amount > balance) log("Insufficient funds")
    else balance -= amount
  }
  // More methods ...
}

object Main extends App {
  //1）new 的时候指定 with ConsoleLogger 特质
//  val acct = new SavingsAccount with ConsoleLogger
  //2）new 一个匿名类，做实现
  val acct = new SavingsAccount {
    override def withdraw(amount: Double): Unit = super.withdraw(amount)
    override def log(msg: String): Unit = {
      println(f"from Anonymous SavingsAccount  $msg")
    }
  }
  acct.withdraw(100)
}

